#
# afkap
#

set -o vi

if test -r $HOME/.afkaprc; then
  . $HOME/.afkaprc
fi

AFKAP_REPO="https://afkap.googlecode.com/svn"

# afkap() - function to provide help, svn subcmds and such
#
afkap() {
  case "$1" in
  "help")
    grep '^#..*()' $HOME/afkap
    ;;
  "svn")
    case "$2" in
      "ls")  svn ls  $AFKAP_REPO;;
      "cat") svn cat $AFKAP_REPO/trunk/afkap.sh;;
    esac
    ;;
  esac
}

# reassign histchars, '%' replaces '^', as it is needed below
#
histchars='!%#'

# shorten shell prompts
#
case "$(id -u)" in
  0) PS1="#; ";;
  *) PS1=":; ";;
esac
PS2=">; "

_AfkapError() {
  echo "afkap: error, $1"
}

# i()  - combo of id(1) and uname(1) w/-n
#
i() {
  echo "UID='$(id -u)'"
  echo "HOST='$(uname -n)'"
}

# l()  - ls(1) w/-F
#
l() {
  ls -F "$@"
}

# ll()  - ls(1) w/-l and -F
#
ll() {
  ls -lF "$@"
}

# c()  - climb and display dir/file tree
#
c() {
  ls -R $1 |
  awk '
    NF == 0 || $0 ~ /\/$/ {
      next;
    }
    /:$/ {
      n = split($0, comps);
      fmt = sprintf("%%%ds%s\n", n * 2, comps[n]);
      printf(fmt, " ");
      next;
    }
    {
      printf("%s%s\n", ident, $0);
    }
  ' -
}

# d()  - overloaded, performs pwd(1), cd(1), mkdir(1), mv(1) or diff(1) w/-u
#
d() {
  case $# in
  0) pwd;;
  1)
    if test -d $1 -o $1 == "-"; then
      cd $1
    else
      _AfkapError "'$1' is not a dir"
    fi
    ;;
  2)
    case "$1" in 
    '+') mkdir $2;;
    '-')
      mv $2 .afkap_rm.$2;
      echo "afkap: it did 'mv $2 .afkap.rm.$2', remove it sometime :-)"
      ;;
    *)
      if test -f $1 -a -f $2; then
        diff -u $1 $2
      fi
      ;;
    esac
    ;;
  esac
}

# aliases() of '^'  - multi shortcuts to cd(1) parent dir[s]
#
alias ^='cd ..; pwd'
alias ^^='cd ../..; pwd'
alias ^^^='cd ../../..; pwd'
alias ^^^^='cd ../../../..; pwd'
alias ^^^^^='cd ../../../../..; pwd'
alias ^^^^^^='cd ../../../../../..; pwd'

# e()  - edit, calls vi(1)
#
e() {
  vi "$@"
}

# p()  - print, overloaded, performs both an echo(1) or printf(1)
#
p() {
  case "$1" in
    %*) printf "$@";;
    *)  echo "$@";;
  esac
}

# s()  - search, scan, overloaded, performs both an egrep(1) or find(1)
#
s() {
  if test -d $1; then
    find "$@"
  else
    egrep "$@"
  fi
}

# t()  - type(1)
#
t() {
  type "$@"
}

# w()  - a better which(1), also follows and displays symlink trail
#
w() {
  CMD="$(type $1 2>&- |sed 's/.* //;s/[()]//g')"  # Command
  case "${CMD}" in
  "")
    _AfkapError "'type $1' failed, not a command"
    ;;
  *)
    IND=""              # init indent
    SYM=""              # init symlink indicator
    while true; do
      echo -n "${IND}${SYM}${CMD}"
      IND="  ${IND}"  # build up indent
      CMD="$(ls -l ${CMD})"
      case "${CMD}" in
      *' -> '*)
        CMD="${CMD#* -> }"
        SYM="-> "
        ;;
      *)
        break
        ;;
      esac
      echo ""
    done
    echo ""
    ;;
  esac
}

gc() {
  if test $# == 0; then
    _AfkapError "gc() requires 1 or more args"
    return 1
  fi
  USER_OPT="--username ssr456@gmail.com"
  case "$1" in
  ls)
    case $# in
      2) svn $USER_OPT ls https://$2.googlecode.com/svn/;;
      *) _AfkapUsage "gc ls projname";;
    esac
    ;;
  wc)
    case $# in
      3) svn $USER_OPT co https://$2.googlecode.com/svn/$3 wc;;
      *) _AfkapUsage "gc co projname dir";;
    esac
    ;;
  esac
}
